const { HTTP } = require('../../utils/http');
const { api } = require('../../utils/api');
const { request } = new HTTP();
const { fromNowFormat } = require('../../utils/filter.js');

// 获取应用实例
const app = getApp()

Page({
  data: {
    vipIcon: '../../images/crownProfile.png',
    noMessage: '../../images/',
    active: 0,
    tabs: [],
    isLoad: true,
    userInfo: {},
    isLogin: false,
    visible: false,
    successVisible: false,
    days: ''
  },
  onShow(){
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 3    // 根据tab的索引值设置
      }) 
    }
    this.setData({
      isLogin: !app.globalData.isLogin,
      userInfo: app.globalData.userInfo
    });
    !app.globalData.isLogin && this._getUserInfo();
    !app.globalData.isLogin && this._queryUserPublish();
  },
  onLoad() {},
  mineInfo() {
    if (!this.data.isLogin) {
      return;
    }
    wx.navigateTo({url: '../mineInfo/index'});
  },

  // 下拉刷新
  onReachBottom() {
    this._getThems();
  },

  publish() {
    wx.navigateTo({url: '../publishNews/index'});
  },

  onTabChange(event) {
    const { detail: {index} } = event;
    this.setData({active: index, isLoad: true});
    this._getThems();
  },

  // 获取用户已发布的话题分类
  async _queryUserPublish() {
    let {returnCode = '', beans = []} = await request({
      url: api.queryUserPublish,
      method: 'get',
      data: {}
    });
    if (returnCode === '0000') {
      beans = beans.map(item => {
        item.themes = [];
        item.page = -1;
        return item;
      })
      this.setData({
        tabs: beans
      });
      if (beans[0].totals > 0) {
        console.log(23);
        this._getThems();
      }
    }
  },
  openVip() {
    this.setData({
      visible: true
    })
  },
  vipClose() {
    this.setData({
      visible: false,
      successVisible: false
    })
  },
  successOpen(event) {
    const { detail: {index} } = event;
    const actions = {
      '0': '1年',
      '1': '半年',
      '2': '30天'
    }
  
    this.setData({
      visible: false,
      successVisible: true,
      days: actions[index]
    })
  },

  async _getThems() {
    if (!this.data.isLoad) return;
    const pageKey = `tabs[${this.data.active}].page`;
    const currentPage = this.data.tabs[this.data.active].page + 1;
    this.setData({[pageKey]: currentPage});

    // 参数
    const category_id = this.data.tabs[this.data.active].id;
    const params = {
      page: currentPage,
      count: 4,
      category_id
    };
    if (!params.category_id) delete params.category_id;

    wx.showNavigationBarLoading();
    wx.setNavigationBarTitle({title: '加载中...'});
    let {returnCode = '', beans = []} = await request({
      url: api.themeUser,
      method: 'get',
      data: params
    });
    wx.hideNavigationBarLoading();
    wx.setNavigationBarTitle({title: '他说'});
    if (returnCode === '0000') {
      if (beans.item.length === 0) {
        this.setData({isLoad: false});
        return;
      }
      beans.item.map(item => {
        item.fromNowFormatTime = fromNowFormat(item.create_time);
        return item;
      });
      const key = `tabs[${this.data.active}].themes`;
      const haveValues = this.data.tabs[this.data.active].themes;
      this.setData({
        [key]: Object.prototype.toString.call(haveValues) == "[object Array]" ? [...haveValues, ...beans.item] : beans.item
      });
    }
  },
  async _getUserInfo() {
    const {returnCode = '', bean = {}} = await request({
      url: api.getUser,
      method: 'get',
      data: {}
    });
    if (returnCode === '0000') {
      app.globalData.userInfo = bean.user;
      this.setData({userInfo: bean.user })
    }
  },
  buyLike() {
    wx.navigateTo({url: '../buyLike/index'});
  }
})
